﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using MyMoviesBrowserLibrary;

namespace MyMoviesBrowserConfiguration
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        MyMoviesDatabase bl = new MyMoviesDatabase();
        
        public Window1()
        {
            InitializeComponent();
            continueButton.Click += new RoutedEventHandler(continueButton_Click);
            cancelButton.Click += new RoutedEventHandler(cancelButton_Click);
            cancelButton2.Click += new RoutedEventHandler(cancelButton_Click);
            saveButton.Click += new RoutedEventHandler(saveButton_Click);
            browseButton.Click += new RoutedEventHandler(browseTrailersFolderButton_Click);

            DataContext = Settings.Default;            
        }

        void saveButton_Click(object sender, RoutedEventArgs e)
        {
            Settings.Default.Save();
            Close();
        }

        void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        void continueButton_Click(object sender, RoutedEventArgs e)
        {
            continueButton.IsEnabled = false;
            statusTextBlock.Text = "Checking Database Connection";
            if (!bl.CheckDataBaseConnection())
            {
                statusTextBlock.Text = "There was an error connecting to the database. Please check the Server Name.";
                errorTextBlock.Text = "Error: " + bl.ErrorMessage;
                continueButton.IsEnabled = true;
                return;
            }
            statusTextBlock.Text = "Checking Database Components";
            if (!bl.CheckAndCreateDatabase())
            {
                statusTextBlock.Text = "There was an error creating the database tables.";
                errorTextBlock.Text = "Error: " + bl.ErrorMessage;
                continueButton.IsEnabled = true;
                return;
            }
            Settings.Default.Save();
            continueButton.IsEnabled = true;

            serverGrid.Visibility = Visibility.Collapsed;
            settingsGrid.Visibility = Visibility.Visible;
            continueButton.IsEnabled = true;
        }

        void browseTrailersFolderButton_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Settings.Default.TrailersDownloadFolder = dialog.SelectedPath;
            }
        }

        
    }
}
